package com.fillifesoft.magicHitter.manager;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.fillifesoft.magicHitter.MagicHitter;

public class SessionManager {

	private SqlSession sqlSession = null;

	public SqlSessionFactory getSqlSessionFactory() {
		InputStream inputStream;
		try {
			inputStream = Resources.getResourceAsStream(MagicHitter.MYBATIS_CONF_FILE);
		}
		catch (IOException e) {
			throw new IllegalArgumentException(e);
		}
		return new SqlSessionFactoryBuilder().build(inputStream);
	}

	public SqlSession getSqlSession() {
		if(sqlSession == null) {
			sqlSession = getSqlSessionFactory().openSession();
		}
		return sqlSession;
	}
}
